package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DeadLetterConfig;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author: atguigu
 * @create: 2023-08-12 09:42
 */
@Slf4j
@Component
public class DeadLetterReceiver {

    /**
     * 监听到死信消息
     * @param msg
     * @param channel
     * @param message
     */
    @SneakyThrows
    @RabbitListener(queues = DeadLetterConfig.queue_dead_2)
    public void receiveDeadLetter(String msg, Channel channel, Message message){
        if(StringUtils.isNotBlank(msg)){
            log.info("【mq监听】,监听到死信实现延迟消息：{}", msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
